package com.example.blog.controller;

import com.example.blog.common.aop.Cache;
import com.example.blog.service.ArticleService;
import com.example.blog.vo.Result;
import com.example.blog.vo.params.ArticleParam;
import com.example.blog.vo.params.PageParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/articles")
@RestController
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    /*如果参数时放在请求体中，application/json传入后台的话，那么后台要用@RequestBody才能接收到；
     如果不是放在请求体中的话，那么后台接收前台传过来的参数时，要用@RequestParam来接收。
     或者形参前 什么也不写也能接收。*/

    @PostMapping("/publish")
    public Result publish(@RequestBody ArticleParam articleParam) {
        return articleService.publish(articleParam);
    }

    /**
     * 首页 文章列表
     *
     * @param pageParams
     * @return
     */
    @PostMapping
    @Cache(expire = 5 * 60 * 1000, name = "list_article")
    @ApiOperation("文章列表接口")
    public Result listArticle(@ApiParam("前端传递的参数") @RequestBody PageParams pageParams) {
        return articleService.listArticle(pageParams);
    }

    /**
     * 首页最热文章
     *
     * @return
     */
    @Cache(expire = 5 * 60 * 1000, name = "hot_article")
    @PostMapping("hot")
    public Result hotArticles() {
        int limit = 6;
        return articleService.hotArticle(limit);
    }

    /**
     * 首页最新文章
     */
    @PostMapping("new")
    @Cache(expire = 5 * 60 * 1000, name = "new_article")
    public Result newArticles() {
        int limit = 5;
        return articleService.newArticle(limit);
    }

    /**
     * 文章归档
     */
    @PostMapping("listArchives")
    @Cache(expire = 5 * 60 * 1000, name = "list_archives")
    public Result listArchives() {
        return articleService.listArchives();
    }

    /**
     * 文章详情
     */
    @PostMapping("/view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId) {
        return articleService.findArticleById(articleId);
    }
}
